package com.iyao_app.adapter;

import android.net.Uri;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.iyao_app.R;
import com.iyao_app.entry.CallList;
import com.iyao_app.util.ImageData;
import com.iyao_app.util.TimeFormat;

import java.util.List;

import butterknife.BindView;
import io.github.rockerhieu.emojicon.EmojiconTextView;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/11.
 */

public class LiuyanAdapter extends BaseQuickAdapter<CallList, BaseViewHolder> {


    public LiuyanAdapter(@Nullable List<CallList> data) {
        super(R.layout.item_liuyan, data);
    }


    @Override
    protected void convert(BaseViewHolder helper, CallList item) {
        SimpleDraweeView sdvHead = helper.getView(R.id.sdv_head);
        EmojiconTextView tvName = helper.getView(R.id.tv_name);
        EmojiconTextView tvContent = helper.getView(R.id.tv_content);
        TextView mTvTime = helper.getView(R.id.tv_time);
        ImageView ivSex = helper.getView(R.id.iv_sex);
        LinearLayout linRoot = helper.getView(R.id.lin_root);
        int position = helper.getLayoutPosition();
        tvName.setText(item.sayHelloFromNickname);
        tvContent.setText(item.commentText);
        mTvTime.setText(TimeFormat.getTime(item.createTime));
        if (item.sayHelloFromSex == 1) {
            ivSex.setImageResource(R.mipmap.ic_boy);
        } else {
            ivSex.setImageResource(R.mipmap.ic_girl);
        }
        linRoot.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        try {
            Uri uri = Uri.parse(item.sayHelloFromAvatarUrl);
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                    .setResizeOptions(new ResizeOptions(80, 80))
                    .build();
            PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                    .setOldController(sdvHead.getController())
                    .setImageRequest(request)
                    .build();
            sdvHead.setController(controller);
        } catch (Exception e) {
            e.printStackTrace();
        }
        helper.addOnClickListener(R.id.sdv_head);
    }
}
